package com.happyscan138.makefun;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.happyscan138.makefun.Fragment.Persionalvideo;
import com.happyscan138.makefun.Fragment.persional_questuion;
import com.happyscan138.makefun.Urls.Urls;
import com.happyscan138.makefun.View.RoundImageView;
import com.happyscan138.makefun.adapter.MyFragmentPagerAdapter;
import com.happyscan138.makefun.base.http;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestHandle;
import com.loopj.android.http.RequestParams;
import com.squareup.picasso.Picasso;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import cz.msebera.android.httpclient.Header;

import static com.happyscan138.makefun.base.http.params;
import static com.loopj.android.http.AsyncHttpClient.log;

/**
 * Created by Administrator on 2017/1/14.
 */

public class SendMessage extends AppCompatActivity implements View.OnClickListener {
    private String user_id;
    private ImageButton send_back;
    private SharedPreferences preferences;
    private SharedPreferences.Editor editor;
    private String access_key;
    private RoundImageView head;
    private TextView nick;
    private TextView sendSign;
    private Button message;
    private ViewPager mPager;
    private ArrayList<Fragment> fragmentsList;
    public String UserID;
    private int currIndex = 0;
    private Intent intent;
    private Fragment question;
    private Fragment video;
    private int position_one;//初始位移
    private int pWidth;//宽度
    public final static int num = 2;
    private int offset ;// 图片偏移量
    private ImageView imagetab;//下划线

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.send_message);
        preferences = getSharedPreferences("UserInfo", MODE_PRIVATE);
        access_key=preferences.getString("UserId","");

        http.client=new AsyncHttpClient();
        http.params=new RequestParams();
        intent=getIntent();
        user_id=intent.getStringExtra("user_id");
        initView();
        InitViewPager();
        InitWidth();
        getData();
        TranslateAnimation animation = new TranslateAnimation(0, offset, 0, 0);
        animation.setFillAfter(true);
        animation.setDuration(300);
        imagetab.startAnimation(animation);
        Log.e("user_iduser_id",user_id);
    }
    public void initView(){
        send_back=(ImageButton)findViewById(R.id.send_back);
        send_back.setOnClickListener(this);
        head=(RoundImageView)findViewById(R.id.send_head);
        nick=(TextView)findViewById(R.id.send_nickName);
        sendSign=(TextView)findViewById(R.id.send_qianming);
        message=(Button)findViewById(R.id.send);
        message.setOnClickListener(this);
        imagetab=(ImageView)findViewById(R.id.myfriend_tab);
    }
    private void InitViewPager( ) {
        Bundle bundle=new Bundle();
        bundle.putString("user_id",user_id);
        mPager = (ViewPager)findViewById(R.id.persional_viewpager);
        question = new persional_questuion();
        log.e("bundle",UserID);
        question.setArguments(bundle);

        video = new Persionalvideo();
        video.setArguments(bundle);
        fragmentsList = new ArrayList<Fragment>();
        fragmentsList.add(question);
        fragmentsList.add(video);

        mPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), fragmentsList));
        mPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                Animation animation = null;
                switch (position) {
                    case 0:
                        if (currIndex == 1) {
                            animation = new TranslateAnimation(position_one, offset, 0, 0);
                        }

                        break;
                    case 1:
                        if (currIndex == 0) {
                            animation = new TranslateAnimation(offset, position_one, 0, 0);
                        }

                        break;
                }
                currIndex = position;
                animation.setFillAfter(true);
                animation.setDuration(300);
                imagetab.startAnimation(animation);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }
        });
        mPager.setCurrentItem(0);
    }
    private void InitWidth( ) {
        BitmapDrawable bd = (BitmapDrawable)getResources().getDrawable(R.mipmap.up_tab);
        Bitmap bmp = bd.getBitmap();
        pWidth = bmp.getWidth();
        DisplayMetrics dm = new DisplayMetrics();
        this.getWindowManager().getDefaultDisplay().getMetrics(dm);
        int screenW = dm.widthPixels;
        offset = (int) ((screenW / num -pWidth)) / 2;//偏移量
        int avg = (int) (screenW / num);
        position_one = avg+offset;//起始位置

    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.send_back:
                finish();
                break;
            case R.id.send:
                Intent intent=new Intent();
                intent.putExtra("receive_u_id",user_id);
                intent.setClass(SendMessage.this,Chat.class);
                startActivity(intent);
                break;
        }
    }
    public void getData(){
        params.put("access_key",access_key);
        params.put("user_id",user_id);
        RequestHandle handle= http.client.post(Urls.MyFriendInfo, params,new JsonHttpResponseHandler(){
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                Log.e("onSuccess",response.toString());
                String jsonStr=response.toString();
                try {
                    JSONObject jsonObject=new JSONObject(jsonStr);
                    JSONObject data=jsonObject.getJSONObject("data");
                    String nickNane=data.getString("nick_name");
                    String sign=data.getString("sign");
                    String headPath= Urls.base+data.getString("img_path");
                    Picasso.with(SendMessage.this).load(headPath).into(head);
                    nick.setText(nickNane);
                    sendSign.setText(sign);

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                super.onFailure(statusCode, headers, responseString, throwable);
                Log.e("onFailure",responseString);
            }
        });
    }
}
